package com.topc.platform.api;

import com.topc.base.api.CommonResult;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.platform.model.po.SignInInfo;
import com.topc.platform.model.po.SignInUserMid;
import com.topc.platform.model.vo.SignInShowVo;
import com.topc.platform.service.ISignInInfoService;
import com.topc.platform.service.ISignInUserMidService;
import com.topc.platform.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/close/sign-in")
public class SignInCloseController {

    @Autowired
    private ISignInInfoService signInInfoService;

    @Autowired
    private ISignInUserMidService signInUserMidService;

    @PostMapping("/search-info")
    public CommonResult<PageResult<SignInInfo>> searchInfo(PageParams pageParams) {
        PageResult<SignInInfo> signInInfoPageResult = signInInfoService.searchInfoByPage(pageParams);
        return CommonResult.success(signInInfoPageResult);
    }

    @PostMapping("/info/add")
    public CommonResult<String> addSignInInfo(@NotNull String action) {
        SignInInfo signInInfo = new SignInInfo();
        signInInfo.setAction(action);
        signInInfoService.save(signInInfo);
        return CommonResult.success("添加成功");
    }

    @GetMapping("/info/delete")
    public CommonResult<String> deleteSignIn(@NotNull Long id) {
        boolean remove = signInInfoService.removeById(id);
        if (!remove) CommonResult.failed("删除失败");
        return CommonResult.success("删除成功");
    }

    @GetMapping("/get-action")
    public CommonResult<SignInShowVo> getAction() {
        SignInShowVo signInShowVo = signInUserMidService.getAction(SecurityUtil.getUser().getNumber());
        return CommonResult.success(signInShowVo);
    }

    @PostMapping("/search-mid")
    public CommonResult<PageResult<SignInUserMid>> searchMidByPage(PageParams params, @NotNull int isToday, @NotNull String userNumber) {
        PageResult<SignInUserMid> pageResult = signInUserMidService.searchMidByPage(params, isToday, userNumber);
        return CommonResult.success(pageResult);
    }

    @GetMapping("/get-history")
    public CommonResult<List<Integer>> getHistory(@NotNull String userNumber){
        List<Integer> history = signInUserMidService.getHistoryCount(userNumber);
        return CommonResult.success(history);
    }

}


